<template>
  <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
    <el-form-item
      prop="dbName"
      label="数据库名："
    >
      <el-input v-model="dynamicValidateForm.dbName"></el-input>
    </el-form-item>
    <el-form-item
      prop="tableName"
      label="表名："
    >
      <el-input v-model="dynamicValidateForm.tableName"></el-input>
    </el-form-item>
    <el-form-item
      prop="tableComment"
      label="表名注释："
    >
      <el-input v-model="dynamicValidateForm.tableComment"></el-input>
    </el-form-item>
    <el-form-item
      v-for="(domain, index) in dynamicValidateForm.domains"
      :label="'字段：' + index"
      :key="domain.key"
      :prop="'domains.' + index + '.value'"
    >
      <el-row class="row-margin">
        <label>字段名称：</label>
        <el-input v-model="domain.fieldName"></el-input>
      </el-row>
      <el-row class="row-margin">
        <label>字段注释：</label>
        <el-input v-model="domain.comment"></el-input>
      </el-row>
      <el-row class="row-margin">
        <label>字段类型：</label>
        <el-input v-model="domain.fieldType"></el-input>
      </el-row>
      <el-row class="row-margin">
        <label>默认值：</label>
        <el-input v-model="domain.defaultValue"></el-input>
      </el-row>
      <el-row class="row-margin">
        <label>是否null：</label>
        <el-input v-model="domain.notNull"></el-input>
      </el-row>
      <el-row class="row-margin">
        <label>是否主键：</label>
        <el-input v-model="domain.primaryKey"></el-input>
      </el-row>
      <el-row class="row-margin">
        <label>是否增长：</label>
        <el-input v-model="domain.autoIncrement"></el-input>
        <el-button @click.prevent="removeDomain(domain)">删除</el-button>
      </el-row>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
      <el-button @click="addDomain">新增字段</el-button>
      <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>

import sqlApi from '@/api/sql'

export default {
  data() {
    return {
      dynamicValidateForm: {
        domains: [{
          fieldName: '',
          comment: '',
          fieldType: '',
          defaultValue: '',
          notNull: '',
          primaryKey: '',
          autoIncrement: ''
        }],
        dbName: '',
        tableName: '',
        tableComment: ''
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          sqlApi.getSqlResult(this.dynamicValidateForm).then((response) => {
            const code = response.code
            // eslint-disable-next-line no-empty
            if (code === 20000) {
              // 成功提示
              this.$message({
                message: response.message,
                type: 'success'
              })
              // 发起下载的请求
              this.download(response.data)
            } else {
              this.$message({
                message: response.message,
                type: 'warning'
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    download(filePath) {
      this.downloadA('http://101.201.64.236:9999/prod-api/sql/download?filePath=' + filePath)
    },
    downloadA(url) {
      const a = document.createElement('a')
      a.href = url
      a.hidden = true
      // a.target = "_blank";
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value1: '',
        value2: '',
        key: Date.now()
      })
    }
  }
}
</script>

<style>
.row-margin {
  margin-bottom: 10px;
}
</style>
